#include <filesystem>
#include <fstream>

namespace fs = std::filesystem;

namespace impl
{
void remove_empty_lines(fs::path filepath)
{
    std::ifstream fin(filepath.native());
    if (!fin)
        throw std::runtime_error("cannot open infile");
    auto tmppath = fs::temp_directory_path() / "temp.txt";
    std::ofstream fout(tmppath.native(), std::ios::out | std::ios::trunc);
    if (!fout)
        throw std::runtime_error("cannot open outfile");

    std::string linebuffer;
    while (std::getline(fin, linebuffer))
    {
        if (linebuffer.length() > 0 &&
            linebuffer.find_first_not_of(" \t") != linebuffer.npos)
        {
            fout << linebuffer << '\n';
        }
    }
    fin.close();
    fout.close();

    fs::remove(filepath);
    fs::rename(tmppath, filepath);
}
}  // namespace impl

using namespace std;
int main(int argc, char** argv)
{
    impl::remove_empty_lines("./test.txt");
    return 0;
}